﻿using System;
using System.Collections.Generic;
using System.Text;
public class NetHandler_KickOutUser : NetMessageHandler
{
    public const string Key = "KickOutUser";

    public static NetHandler_KickOutUser NewHandler()
    {
        return new NetHandler_KickOutUser();
    }

    protected override ISerialize NewData() => new KickOutData();

    public override void Execute(Remote client, ISerialize _data)
    {
        KickOutData kickOutData = _data as KickOutData;

        GameDebug.Log($"Master  请求踢出用户：{kickOutData.userId}");

        ResponseData response = new ResponseData();
        response.code = 0;

        if (kickOutData.userId == -1)
        {
            GameDebug.Log("踢出所有用户");
            Game.instance.userCollection.ForeachAction((user) =>
            {
                  NetEvent_KickOut netEvent_KickOut = new NetEvent_KickOut();
                  if (user != null && user.client != null)
                  {
                      netEvent_KickOut.SendToRemote(user.client);
                  }
            });
            Game.instance.userCollection.ClearAllUsers();
        }
        else
        {
            NetEvent_KickOut netEvent_KickOut = new NetEvent_KickOut();

            UserServer user = Game.instance.GetUser(kickOutData.userId);
            if (user != null)
            {
                netEvent_KickOut.SendToRemote(user.client);

                Game.instance.RemoveUser(user.client);
            }
            else
            {
                GameDebug.LogError($"找不到用户：{kickOutData.userId}");
                response.code = -1;
                response.err = "找不到用户";
            }
        }

        
        byte[] senddata = response.Write();

        this.SendResponse(client, Key, this.m_MessageReceived.seq, senddata);
    }
}

